package xyz.labmem.main.modular.common.service

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import xyz.labmem.base.core.tool.getKClassAllField
import xyz.labmem.base.core.tool.isNotBlank
import xyz.labmem.main.modular.common.pojo.QueryMap
import xyz.labmem.main.modular.common.pojo.QueryType
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.reflect.KClass

/**
 * @Author lty
 * @Date 2022/2/15 17:08
 */
fun <T> selectFormPage(query: List<QueryMap>?): QueryWrapper<T> {
    return QueryWrapper<T>().apply {
        query?.forEach {
            if (it.field.isNotBlank() && it.value != null) {
                when (it.type) {
                    QueryType.LIKE -> this.like("`${it.field}`", it.value)
                    QueryType.EQ -> this.eq("`${it.field}`", it.value)
                    QueryType.LT -> this.lt("`${it.field}`", it.value)
                    QueryType.GT -> this.gt("`${it.field}`", it.value)
                    QueryType.DESC -> this.orderByDesc("`${it.field}`")
                    QueryType.ASC -> this.orderByAsc("`${it.field}`")
                }
            }
        }
    }
}

fun <T : Any> selectKtQFormPage(clazz: KClass<T>, query: List<QueryMap>?): KtQueryWrapper<T> {
    val ktq = KtQueryWrapper(clazz.java)
    clazz.getKClassAllField().forEach {
        query?.forEach { q ->
            if (q.field.isNotBlank() && it.name == q.field) {
                when (q.type) {
                    QueryType.LIKE -> ktq.like(q.value != null, it, q.value)
                    QueryType.EQ -> ktq.eq(q.value != null, it, q.value)
                    QueryType.LT -> ktq.lt(q.value != null, it, q.value)
                    QueryType.GT -> ktq.gt(q.value != null, it, q.value)
                }
            }
        }
    }
    return ktq
}

fun <T> selectFormMap(query: List<QueryMap>?): QueryWrapper<T> {

    return QueryWrapper<T>().apply {
        query?.forEach {
            if (it.field.isNotBlank() && it.value != null) {
                if(it.field.equals("tjsj")){
                    val myDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
                    var endDate = myDateTimeFormatter.format(LocalDateTime.now())+" 23:59:59"
                    val cal = Calendar.getInstance()
                    cal.add(Calendar.YEAR,-it.value!!.toInt());
                    val myLdt = LocalDateTime.of(cal.get(Calendar.YEAR), 1, 1,0,0)
                    var startDate = myDateTimeFormatter.format(myLdt)

                    this.gt("tbrq", startDate)
                    this.lt("tbrq", endDate)
                    this.orderByAsc("tbrq")
                }else{
                    when (it.type) {
                        QueryType.LIKE -> this.like("`${it.field}`", it.value)
                        QueryType.EQ -> this.eq("`${it.field}`", it.value)
                        QueryType.LT -> this.lt("`${it.field}`", it.value)
                        QueryType.GT -> this.gt("`${it.field}`", it.value)
                        QueryType.DESC -> this.orderByDesc("`${it.field}`")
                        QueryType.ASC -> this.orderByAsc("`${it.field}`")
                    }
                }
            }
        }
    }
}